package com.mashibing.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mashibing.internalcommon.request.DriverUserWorkStatus;
import com.mashibing.internalcommon.constant.DriverCarConstants;
import com.mashibing.internalcommon.dto.DriverUser;
import com.mashibing.internalcommon.dto.ResponseResult;
import com.mashibing.internalcommon.response.DriverUserExistsResponse;
import com.mashibing.mapper.DriverUserMapper;
import com.mashibing.mapper.DriverUserWorkStatusMapper;
import com.mashibing.service.DriverUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class DriverUserServiceImpl implements DriverUserService {

    @Autowired
    private DriverUserMapper driverUserMapper;

    @Autowired
    private DriverUserWorkStatusMapper driverUserWorkStatusMapper;

    @Override
    public ResponseResult updateUser(DriverUser driverUser) {
        LocalDateTime now = LocalDateTime.now();
        driverUser.setGmtModified(now);
        driverUserMapper.updateById(driverUser);
        return ResponseResult.success();
    }

    @Override
    public ResponseResult<DriverUserExistsResponse> verificationDriverUser(String phoneNumber) {

        QueryWrapper<DriverUser> wrapper = new QueryWrapper<>();
        wrapper.eq("driver_phone",phoneNumber);
        wrapper.eq("state", DriverCarConstants.DRIVER_STATE_VALID);
        DriverUser driverUser = driverUserMapper.selectOne(wrapper);

        DriverUserExistsResponse existsResponse = new DriverUserExistsResponse();
        if (driverUser == null) {
            existsResponse.setIfExist(DriverCarConstants.DRIVER_NOT_EXISTS);
        }else {
            existsResponse.setIfExist(DriverCarConstants.DRIVER_EXISTS);
        }
        return ResponseResult.success(existsResponse);
    }

    @Override
    public ResponseResult addDriverUser(DriverUser driverUser) {
        LocalDateTime now = LocalDateTime.now();
        driverUser.setGmtCreate(now);
        driverUser.setGmtModified(now);

        driverUserMapper.insert(driverUser);
        // 初始化 司机工作状态表
        DriverUserWorkStatus driverUserWorkStatus = new DriverUserWorkStatus();
        driverUserWorkStatus.setDriverId(driverUser.getId());
        driverUserWorkStatus.setWorkStatus(DriverCarConstants.DRIVER_WORK_STATUS_STOP);
        driverUserWorkStatus.setGmtModified(now);
        driverUserWorkStatus.setGmtCreate(now);
        driverUserWorkStatusMapper.insert(driverUserWorkStatus);


        return ResponseResult.success("");
    }

}
